package com.xlf.common.core.util.file;

import com.qiniu.util.Auth;
import com.xlf.common.core.properties.QinIuProperties;
import lombok.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 */
@Component
@Data
public class QinIuUtils {

   @Autowired
   private QinIuProperties qinIuProperties;

    /**
     * https://developer.qiniu.com/kodo/1239/java
     * @return
     */
    public String getToken(){
        Auth auth = Auth.create(qinIuProperties.getAccessKey(), qinIuProperties.getSecretKey());
        String upToken = auth.uploadToken(qinIuProperties.getBucket());
        return upToken;
    }

    /**
     * 上传信息
     * @return
     */
    public QinIuVO buildUpload(){
        QinIuVO qinIuVO = new QinIuVO();
        qinIuVO.setToken(getToken());
        qinIuVO.setUrl(qinIuProperties.getUrl());
        qinIuVO.setRegion(qinIuProperties.getRegion());
        return qinIuVO;
    }


    /**
     * 上传区域
     */
    @Deprecated
    @Getter
    @AllArgsConstructor
    public enum region {
        z0("z0","z0"),
        z1("z1","z1"),
        na0("na0","na0"),
        as0("as0","as0"),
        cnEast2("cnEast2","cnEast2");
        /**
         * 类型
         */
        private final String type;

        /**
         * 描述
         */
        private final String des;
    }


    /**
     * 前端vo
     */
    @Data
    public class QinIuVO implements Serializable {

        private String token;

        private String url;

        /**
         * 上传区域
         */
        private String region;
    }

}
